package com.encryptdemo.encrypt2.config;


import com.encryptdemo.encrypt.util.AESUtil;
import com.encryptdemo.encrypt2.annotation.Decrypt;
import com.encryptdemo.encrypt2.domain.EncryptProperties;
import com.encryptdemo.encrypt2.util.AESUtil2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

//允许获取EncryptProperties配置类
@EnableConfigurationProperties(EncryptProperties.class)
//@ControllerAdvice是一个全局数据处理组件
@ControllerAdvice

/**
* 实现的不是RequestBodyAdvice，而是RequestBodyAdviceAdapter
 * RequestBodyAdviceAdapter继承RequestBodyAdvice
*/
public class DecryptRequest extends RequestBodyAdviceAdapter {

    //引入加密属性类 主要是为了获取配置文件中的密钥
    @Autowired
    private EncryptProperties encryptProperties;


    /**
    * 配置支持条件
     * 这里是只有方法或者参数有Decrypt注解的时候才生效
    */
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return methodParameter.hasMethodAnnotation(Decrypt.class)||methodParameter.hasParameterAnnotation(Decrypt.class);
    }


    /**
    * 使用aop，在读取请求参数的之前，对请求参数进行解密
    */
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {

        //获取密钥的字节
        byte[] keyByte = encryptProperties.getKey().getBytes();
        //读取请求参数成为字节body
//        byte[] body = new byte[inputMessage.getBody().available()];
        byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());

        inputMessage.getBody().read(body);

        try {
            //解密
            byte[] decrypt = AESUtil2.decrypt(body, keyByte);
            //将解密的字节放入字节数组输入流中
            ByteArrayInputStream bais = new ByteArrayInputStream(decrypt);

            //返回HttpInputMessage
            return new HttpInputMessage() {
                @Override
                public InputStream getBody() throws IOException {
                    return bais;
                }

                @Override
                public HttpHeaders getHeaders() {
                    return inputMessage.getHeaders();
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }

        //如果不需要进行处理的话，则直接调用父类的beforeBodyRead，相当于直接返回inputMessage，不做处理
        return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);
    }
}


